package com.stars.home.repository

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import com.stars.home.activity.MainActivity
import com.stars.home.activity.RegisterActivity
import com.stars.home.model.LoginBean
import com.stars.home.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRepository
import com.trailer.base.utils.UiSwitch

class OneLoginRepository : BRepository() {
    @SuppressLint("CheckResult")
    fun login(code: String) {

        rxLreq(NetEngine.service.wxLogin(code))
                .doOnError() {
                    it.printStackTrace()
                }
                .doOnNext {
                    if (it.ok(true)) {

                        val bean: LoginBean = it.data() as LoginBean;
                        MMKV.defaultMMKV().putString("token", bean.token)

                        UiSwitch.single(context, MainActivity::class.java)

                        (context as Activity).finish()

                    } else {
                        if (it.code() == 0) {
                            UiSwitch.bundle(context, RegisterActivity::class.java, Bundle().apply {
                                putString("openid","")
                                putString("unionid", "")
                            })
                        }
                    }
                }
                .subscribe();

    }


    @SuppressLint("CheckResult")
    fun simLogin(mobile: String, type: String) {

        rxLreq(NetEngine.service.simlRegister(mobile, type))
                .doOnNext {
                    if (it.ok(true)) {
                        var bean: LoginBean = it.data() as LoginBean;
                        MMKV.defaultMMKV().putString("token", bean.token)
                        UiSwitch.single(context, MainActivity::class.java)
                        (context as Activity).finish()

                    }
                }
                .subscribe();

    }


}
